package com.fhc.laser_monitor_sw_android_rtsp_app;

import static org.greenrobot.eventbus.EventBus.TAG;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.blankj.utilcode.util.Utils;
import com.fhc.laser_monitor_sw_android_rtsp_app.utils.HomeKeyEventReceiver;
import com.tencent.mmkv.MMKV;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyApplication extends Application {

    public static final String RTSP_KEY = "6D75724D7A4A36526D343241644A35646F6D466B634E5A6A623230755A6D686A4C6D78686332567958323176626D6C3062334A66633364665957356B636D39705A46397964484E77583246776346634D56714459384F4A4659584E355247467964326C755647566862556C7A5647686C516D567A64434D794D4445354F57566863336B3D";

    private static MMKV kv;

    // 创建一个定长线程池，支持定时及周期性任务执行
    private static ScheduledExecutorService executor;

    //单一线程的线程池
    private static ThreadPoolExecutor singleThreadExecutor;

    private static Context context;

    @Override
    public void onCreate() {

        MyApplication.context = getApplicationContext();

        super.onCreate();

        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "找不到内部OpenCV库。使用OpenCV管理器进行初始化  ----  Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "在包内找到OpenCV库。使用它！ ---  OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }

        Utils.init(this);

        MMKV.initialize(this);

        kv = MMKV.defaultMMKV();

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(new HomeKeyEventReceiver(), intentFilter);
    }

    public static ExecutorService getSingleThreadExecutor() {
        if (singleThreadExecutor == null) {
            //单一线程的线程池
            singleThreadExecutor = new ThreadPoolExecutor(1, 1,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>());
        }
        return singleThreadExecutor;
    }


    //获取定时执行服务
    public static ScheduledExecutorService getScheduledExecutorService() {
        if (executor == null) {
            executor = new ScheduledThreadPoolExecutor(9);
        }
        return executor;
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }

    public static MMKV getMMKV() {
        return MyApplication.kv;
    }

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            // TODO Auto-generated method stub
            switch (status) {
                case BaseLoaderCallback.SUCCESS:
                    Log.i(TAG, "成功加载opencv");
//                    Toast toast = Toast.makeText(getApplicationContext(),
//                            "成功加载opencv！", Toast.LENGTH_SHORT);
//                    toast.setGravity(Gravity.CENTER, 0, 0);
                    //toast.show();
                    break;
                default:
                    super.onManagerConnected(status);
                    Log.i(TAG, "加载失敗");
//                    Toast toast1 = Toast.makeText(getApplicationContext(),
//                            "加载失敗！", Toast.LENGTH_SHORT);
//                    toast1.setGravity(Gravity.CENTER, 0, 0);
//                    toast1.show();
                    break;
            }

        }
    };
}
